#include "..\..\include\guiappimage.h"

GUIAppImage::GUIAppImage(int _id, float x, float y, char* file) : GUIAppObject () {
	bStatic = true;
	bVisible = true;
	bEnabled = true;
	id=_id;
	
	HTEXTURE tex = hge->Texture_Load(file);
	
	m_Width  = (float)hge->Texture_GetWidth(tex);
	m_Height = (float)hge->Texture_GetHeight(tex);

	float m_rWidth  = (float)hge->Texture_GetWidth(tex, true);
	float m_rHeight = (float)hge->Texture_GetHeight(tex, true);

	rect.Set(x, y, x + m_rWidth, y + m_rHeight);
	
	for(int i=0;i<4;i++)
	{
		m_Quad.v[i].z=0;
		m_Quad.v[i].col=0xFFFFFFFF;
	}
	
	m_Quad.blend = BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_ZWRITE;
	m_Quad.tex = tex;
	
	m_Quad.v[0].x = x;					m_Quad.v[0].y = y; 
	m_Quad.v[1].x = x+m_Width;			m_Quad.v[1].y = y; 
	m_Quad.v[2].x = x+m_Width;			m_Quad.v[2].y = y+m_Height; 
	m_Quad.v[3].x = x;					m_Quad.v[3].y = y+m_Height;
	
	m_Quad.v[0].tx = 0;					m_Quad.v[0].ty = 0;
	m_Quad.v[1].tx = 1;					m_Quad.v[1].ty = 0;
	m_Quad.v[2].tx = 1;					m_Quad.v[2].ty = 1;
	m_Quad.v[3].tx = 0;					m_Quad.v[3].ty = 1;
}


void GUIAppImage::Scale(float w, float h) {
	if ( w ) {
		m_Quad.v[1].x = m_Quad.v[0].x + w;
		m_Quad.v[2].x = m_Quad.v[0].x + w;
		m_Width = w;
	}
	if ( h ) {
		m_Quad.v[2].y = m_Quad.v[0].y + h;
		m_Quad.v[3].y = m_Quad.v[0].y + h;
		m_Height = h;
	}
	rect.Set(m_Quad.v[0].x, m_Quad.v[0].y, m_Quad.v[2].x, m_Quad.v[2].y);
}

GUIAppImage::~GUIAppImage() {
	hge->Texture_Free(m_Quad.tex);
}

void GUIAppImage::Render() {
	hge->Gfx_RenderQuad(&m_Quad);
}

void GUIAppImage::SetPos(float x, float y)
{
	m_Quad.v[0].x = x;					m_Quad.v[0].y = y; 
	m_Quad.v[1].x = x+m_Width;	m_Quad.v[1].y = y; 
	m_Quad.v[2].x = x+m_Width;	m_Quad.v[2].y = y+m_Height; 
	m_Quad.v[3].x = x;					m_Quad.v[3].y = y+m_Height;
}

void GUIAppImage::Move(float dx, float dy) {
	rect.Set(rect.x1-dx,rect.y1-dy,rect.x2-dx,rect.y2-dy);
	SetPos(rect.x1,rect.y1);
}
